//=============================================================================
//     _   _                               ____                _  _          
//    | | | |  __ _  _ __   _ __   _   _  / ___|  _ __   _ __ (_)| |_   ___ 
//    | |_| | / _` || '_ \ | '_ \ | | | | \___ \ | '_ \ | '__|| || __| / _ \
//    |  _  || (_| || |_) || |_) || |_| |  ___) || |_) || |   | || |_ |  __/
//    |_| |_| \__,_|| .__/ | .__/  \__, | |____/ | .__/ |_|   |_| \__| \___|
//                  |_|    |_|     |___/         |_|                         
//
//                     HappySprite - We make sprites happy
//
// Copyright (c) 2007 by Tank Monkey Games
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//=============================================================================

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;

namespace HappySprite.SceneGraph
{
    class WorldTransform : Transform3D
    {
        SceneItem owner;
        bool transformInvalid;

        internal WorldTransform(SceneItem owner)
            : base()
        {
            this.owner = owner;
        }

        public override void CopyFrom(Transform3D transform)
        {
            base.CopyFrom(transform);
            transformInvalid = false;
        }

        protected override void OnTransformChanged()
        {
            transformInvalid = false;
            owner.InvalidateChildern();
        }

        protected override void UpdateTransform()
        {
            if (transformInvalid)
            {
                Transform3D local = owner.Local;

                if (owner.Parent == null || owner.Parent.World.IsKnownToBeIdentity)
                {
                    // Parent world is identity or no parent
                    CopyFromCore(local);
                }
                else
                {
                    Transform3D parent = owner.Parent.World;

                    // Update rotation
                    if (owner.InheritRotation)
                    {
                        // Combine orientation with that of parent
                        SetRotation(local.Rotation.Quaternion * parent.Rotation.Quaternion);
                    }
                    else
                    {
                        // No inheritance
                        SetRotation(local.Rotation.Quaternion);
                    }

                    // Update scale
                    if (owner.InheritScale)
                    {
                        // Scale own position by parent scale
                        SetScale(local.Scale * parent.Scale);
                    }
                    else
                    {
                        // No inheritance
                        SetScale(local.Scale);
                    }

                    // Update position
                    SetPosition(Vector3.Transform(parent.Scale * local.Position, parent.Rotation.Quaternion));
                }

                transformInvalid = false;
            }
        }

        internal void Invalidate()
        {
            transformInvalid = true;
            IsKnownToBeIdentity = false;
        }
    }
}
